package org.imac.quizz;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ResultActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.result);
        
        TextView pseudoText = (TextView) findViewById(R.id.score);
        
       //On recupere l'Intent que l'on a recu
        Intent thisIntent = getIntent();
        
        //On recupere les deux extra grace a leurs id        
        int score = thisIntent.getIntExtra("score", -1);
        int maxScore = thisIntent.getIntExtra("maxScore", -1);
        int result = (int)((double)score/(double)maxScore*100);
       if (result<20){ pseudoText.setText("La mode c'est pas ton truc! Tu as obtenu : "+ result + "%"); }
        else if (result<50){ pseudoText.setText("Il y a encore des progrès à faire, tu as obtenu : "+ result + "%"); }
        else if (result<80){ pseudoText.setText("C'est pas mal! Tu as obtenu : "+ result + "%"); }
        else { 
        	pseudoText.setText("Félicitations tu as obtenu : "+ result + "%"); 
        }
        
        Resources res = getResources();
        StateListDrawable myButtonRejoue = new StateListDrawable();
        myButtonRejoue.addState(new int[] {android.R.attr.state_pressed}, res.getDrawable(R.drawable.bouton_rejouer_2));
        myButtonRejoue.addState(new int[] {}, res.getDrawable(R.drawable.bouton_rejouer));        
        ((Button)findViewById(R.id.boutonRejouer)).setBackgroundDrawable(myButtonRejoue);        
        ((Button)findViewById(R.id.boutonRejouer)).setOnClickListener(new View.OnClickListener() {        	
            public void onClick(View v) {
                Intent nbQIntent = getIntent();
            	Intent intent = new Intent(ResultActivity.this, InscriptionActivity.class);
            	intent.putExtra("nbQC", nbQIntent.getExtras().getInt("nbQC"));
            	startActivity(intent);
            }
        });
        StateListDrawable myButton2 = new StateListDrawable();
        myButton2.addState(new int[] {}, res.getDrawable(R.drawable.bouton_retour));        
        ((Button)findViewById(R.id.buttonMenu)).setBackgroundDrawable(myButton2);        
        ((Button)findViewById(R.id.buttonMenu)).setOnClickListener(new View.OnClickListener() {        	
            public void onClick(View v) {
            	Intent intent = new Intent(ResultActivity.this, QuizzActivity.class);
            	startActivity(intent);
            }
        });  
        
    }
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.xml.menu, menu);
        return true;
     }
     // Called when menu item is selected 
     public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.itemPrefs:
          // Launch Prefs activity
          Intent i = new Intent(ResultActivity.this, PreferenceQuizz.class);
          startActivity(i);
          break;
        }
        return true;
     }
}

        
      
